﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.IO;

namespace SDB
{
    public class GetFileSystemInformationConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (value is DriveInfo)
                {
                    DirectoryInfo di = new DirectoryInfo(((DriveInfo)value).RootDirectory.Name);
                    return GetAccessibleDirectories(di);
                }
                else if (value is DirectoryInfo)
                {
                    return GetAccessibleDirectories(((DirectoryInfo)value));
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        private List<DirectoryInfo> GetAccessibleDirectories(DirectoryInfo parent)
        {
            List<DirectoryInfo> all_directories = new List<DirectoryInfo>();
            foreach (DirectoryInfo info in parent.GetDirectories())
            {
                try
                {
                    info.GetFiles();
                    all_directories.Add(info);
                }
                catch (UnauthorizedAccessException)
                {
                }
            }
            return all_directories;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
 	        throw new NotImplementedException();
        }

        #endregion
    }
}